package ma.myschool.app.model;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

@Entity
@Table(name = "paiement")
public class Paiement extends BaseObject {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3018588558392962319L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "paiement_id")
	private Long id;

	@Column(name = "date_paiement")
	private Date datePaiement = Calendar.getInstance().getTime();
	
	@ManyToOne(fetch=FetchType.EAGER)
	@JoinColumn(name="type_paiement_id", nullable = false)
	private TypePaiement typePaiement;
	
	@ManyToOne(fetch=FetchType.EAGER)
	@JoinColumn(name="mode_paiement_id", nullable = false)
	private ModePaiement modePaiement = new ModePaiement();
	
	@Column(name = "numero_cheque", length=30)
	private String numeroCheque;
	
	@Column(name = "avance")
	private float avance;
	
	@Column(name = "reste")
	private float reste;
	
	@Column(name = "avance_justifie")
	private boolean avanceJustifie;
	
	@Column(name = "description_avance_justifie")
	private String descriptionAvanceJustifie;
	
	@ManyToOne(fetch=FetchType.EAGER)
	@JoinColumn(name="eleve_id", nullable = false)
	private Eleve eleve = new Eleve();
	
	@ManyToOne(fetch=FetchType.EAGER)
	@JoinColumn(name="annee_scolaire_id", nullable = false)
	private AnneeScolaire anneeScolaire = new AnneeScolaire();
	
	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name="mois_paye_id")
	private MoisPaye moisPaye = new MoisPaye();

	@Transient
	private List<PaiementHistory> paiementHistory;
	
	public Paiement() {
		super();
	}

	public Paiement(Long id, Date datePaiement, TypePaiement typePaiement,
			String numeroCheque, String descriptionAvanceJustifie) {
		super();
		this.id = id;
		this.datePaiement = datePaiement;
		this.typePaiement = typePaiement;
		this.numeroCheque = numeroCheque;
		this.descriptionAvanceJustifie = descriptionAvanceJustifie;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Date getDatePaiement() {
		return datePaiement;
	}

	public void setDatePaiement(Date datePaiement) {
		this.datePaiement = datePaiement;
	}

	public TypePaiement getTypePaiement() {
		return typePaiement;
	}

	public void setTypePaiement(TypePaiement typePaiement) {
		this.typePaiement = typePaiement;
	}

	public ModePaiement getModePaiement() {
		return modePaiement;
	}

	public void setModePaiement(ModePaiement modePaiement) {
		this.modePaiement = modePaiement;
	}

	public String getNumeroCheque() {
		return numeroCheque;
	}

	public void setNumeroCheque(String numeroCheque) {
		this.numeroCheque = numeroCheque;
	}

	public String getDescriptionAvanceJustifie() {
		return descriptionAvanceJustifie;
	}

	public void setDescriptionAvanceJustifie(String descriptionAvanceJustifie) {
		this.descriptionAvanceJustifie = descriptionAvanceJustifie;
	}

	public Eleve getEleve() {
		return eleve;
	}

	public void setEleve(Eleve eleve) {
		this.eleve = eleve;
	}
	
	public float getAvance() {
		return avance;
	}

	public void setAvance(float avance) {
		this.avance = avance;
	}

	public float getReste() {
		return reste;
	}

	public void setReste(float reste) {
		this.reste = reste;
	}

	public boolean isAvanceJustifie() {
		return avanceJustifie;
	}

	public void setAvanceJustifie(boolean avanceJustifie) {
		this.avanceJustifie = avanceJustifie;
	}

	public MoisPaye getMoisPaye() {
		return moisPaye;
	}

	public void setMoisPaye(MoisPaye moisPaye) {
		this.moisPaye = moisPaye;
	}

	
	public AnneeScolaire getAnneeScolaire() {
		return anneeScolaire;
	}

	public void setAnneeScolaire(AnneeScolaire anneeScolaire) {
		this.anneeScolaire = anneeScolaire;
	}
	
	public List<PaiementHistory> getPaiementHistory() {
		return paiementHistory == null ? new ArrayList<PaiementHistory>() : paiementHistory;
	}

	public void setPaiementHistory(List<PaiementHistory> paiementHistory) {
		this.paiementHistory = paiementHistory;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + Float.floatToIntBits(avance);
		result = prime * result + (avanceJustifie ? 1231 : 1237);
		result = prime * result
				+ ((datePaiement == null) ? 0 : datePaiement.hashCode());
		result = prime * result
				+ ((descriptionAvanceJustifie == null) ? 0 : descriptionAvanceJustifie.hashCode());
		result = prime * result + ((eleve == null) ? 0 : eleve.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result
				+ ((modePaiement == null) ? 0 : modePaiement.hashCode());
		result = prime * result
				+ ((moisPaye == null) ? 0 : moisPaye.hashCode());
		result = prime * result
				+ ((numeroCheque == null) ? 0 : numeroCheque.hashCode());
		result = prime * result + Float.floatToIntBits(reste);
		result = prime * result
				+ ((typePaiement == null) ? 0 : typePaiement.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Paiement other = (Paiement) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "Paiement [id=" + id + ", datePaiement=" + datePaiement
				+ ", typePaiement=" + typePaiement + ", modePaiement="
				+ modePaiement + ", numeroCheque=" + numeroCheque + ", avance="
				+ avance + ", reste=" + reste + ", avanceJustifie="
				+ avanceJustifie + ", descriptionAvanceJustifie=" + descriptionAvanceJustifie + ", eleve="
				+ eleve + ", moisPaye=" + moisPaye + "]";
	}
	
}
